<template>
  <view class="dept-users-container">
    <!-- 顶部导航 -->
    <view class="navbar">
      <text class="back-btn" @tap="goBack">‹</text>
      <text class="title">{{ deptName || '部门成员' }}</text>
    </view>
    
    <!-- 用户列表 -->
    <view class="user-list">
      <view 
        class="user-item" 
        v-for="user in userList" 
        :key="user.id"
        @tap="goToUserDetail(user)"
      >
        <view class="user-avatar">
          {{ user.username ? user.username.charAt(0).toUpperCase() : 'U' }}
        </view>
        <view class="user-info">
          <view class="user-name">{{ user.username || '未知用户' }}</view>
          <view class="user-position">{{ user.position || '暂无职位' }}</view>
        </view>
      </view>
      <view v-if="userList.length === 0 && !loading" class="empty-tip">
        暂无部门成员
      </view>
    </view>
    
    <!-- 加载状态 -->
    <view v-if="loading" class="loading-mask">
      <text class="loading-text">加载中...</text>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      deptId: '',
      deptName: '',
      userList: [],
      loading: false
    }
  },
  
  methods: {
    // 返回上一页
    goBack() {
      uni.navigateBack()
    },
    
    // 获取部门用户
    async loadDeptUsers() {
      this.loading = true
      try {
        const response = await this.$api.system.dept.getDeptUsers(this.deptId)
        if (response.code === 200) {
          this.userList = response.data || []
        } else {
          uni.showToast({
            title: response.msg || '获取部门用户失败',
            icon: 'none'
          })
        }
      } catch (error) {
        console.error('加载部门用户失败:', error)
        uni.showToast({
          title: '网络错误',
          icon: 'none'
        })
      } finally {
        this.loading = false
      }
    },
    
    // 跳转到用户详情
    goToUserDetail(user) {
      uni.navigateTo({
        url: `/pages/contacts/user-detail?id=${user.id}`
      })
    }
  },
  
  onLoad(options) {
    this.deptId = options.deptId || ''
    this.deptName = options.deptName || ''
    this.loadDeptUsers()
  }
}
</script>

<style lang="scss" scoped>
.dept-users-container {
  min-height: 100vh;
  background-color: #f5f5f5;
}

.navbar {
  display: flex;
  align-items: center;
  background-color: #fff;
  padding: 30rpx;
  border-bottom: 1rpx solid #eee;
}

.back-btn {
  font-size: 40rpx;
  color: #333;
  margin-right: 30rpx;
}

.title {
  font-size: 36rpx;
  font-weight: bold;
  color: #333;
}

.user-list {
  background-color: #fff;
  padding: 0 30rpx;
}

.user-item {
  display: flex;
  align-items: center;
  padding: 30rpx 0;
  border-bottom: 1rpx solid #f5f5f5;
  
  &:last-child {
    border-bottom: none;
  }
}

.user-avatar {
  width: 80rpx;
  height: 80rpx;
  border-radius: 50%;
  background-color: #1890ff;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 32rpx;
  font-weight: bold;
  margin-right: 20rpx;
}

.user-info {
  flex: 1;
}

.user-name {
  font-size: 32rpx;
  color: #333;
  margin-bottom: 8rpx;
}

.user-position {
  font-size: 26rpx;
  color: #999;
}

.empty-tip {
  text-align: center;
  padding: 100rpx 0;
  color: #999;
  font-size: 28rpx;
}

.loading-mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.1);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 999;
}

.loading-text {
  color: #666;
  font-size: 28rpx;
}
</style>